package com.example.springaidemo.controller.case5;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author stone
 * @date 2025/7/4 10:40
 */
@RestController
@RequestMapping("/case5")
@Slf4j
public class Case5Controller {

    public Case5Controller(ZhiPuAiChatModel chatModel,
                           ChatMemory chatMemory,
                           OrderTools orderTools) {
        this.chatClient = ChatClient
                .builder(chatModel)
                .defaultAdvisors(
                        PromptChatMemoryAdvisor.builder(chatMemory).build()
                )
                .defaultTools(orderTools)
                .build();
    }

    private ChatClient chatClient;

    /**
     * tools使用
     */
    @GetMapping("/chat")
    public String chat(@RequestParam("input") String input) {
        // input=我要退订
        // input=账号是101，订单号是XXX1111
        return this.chatClient.prompt()
                .user(input)
                .call()
                .content();
    }
}
